#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>

static void __genData(char *buf)
{
    int i, n;
    for (i = 0, n = 0; i < 6; i++)
    {
        buf[n++] = 0xFF;
    }
    for (i = 0; i < 16; i++)
    {
        buf[n++] = 0x70;
        buf[n++] = 0x85;
        buf[n++] = 0xC2;
        buf[n++] = 0x01;
        buf[n++] = 0xAE;
        buf[n++] = 0xF7;
    }
}

int main(int argc, char *argv[])
{
    int fd;
    struct sockaddr_in addr;
    char buf[102] = "";

    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd < 0)
    {
        perror("socket");
        exit(1);
    }

    int on = 1;
    if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0)
    {
        perror("setsockopt SO_BROADCAST");
        goto CleanUp;
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(7);
    addr.sin_addr.s_addr = inet_addr("255.255.255.255"); // 255.255.255.255

    __genData(buf);
    int i;
    for (i = 0; i < sizeof(buf); i++)
    {
        printf("%02X ", buf[i] & 0xFF);
        if (i && ((i+1) % 6 == 0))
        {
            printf("\n");
        }
    }
    for (i = 1; i <= 10; i++)
    {
        int n = sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *) &addr, sizeof(addr));
        printf("[%d] send length=%d\n", i, n);
    }

CleanUp:
    close(fd);
    return 0;
}
